<?php
class hschool_class{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
	public static $all_class_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}

	/*
	*	取出一个班级信息
	*/
	public function get_one_class($class_id='') {
		$info = array();
		if($class_id){
			$sql = "SELECT * FROM ".DB::table('class')." WHERE class_id='".$class_id."' LIMIT 1";
			$info = DB::fetch_first($sql);
		}
		return $info;
	}

	/*
	*	根据机构dept_id 获取所有班级
	*/
	public function get_all_class() {
		global $_G; 
		//DEBUG 是否是 班主任 如果是班主任 则取出所管理的班级
		$get_all_class_sql = "SELECT * FROM ".DB::table('class')." WHERE isdelete = 0 AND dept_id='".$_G['member']['dept_id']."' ORDER BY class_id DESC";
		$get_all_class = DB::fetch_all($get_all_class_sql);
		return $get_all_class;
	}

	/*
	*	获取学校的班级
	*/
	public function get_dept_class_option() {
		global $_G;
		$option='';
		$d_id = !empty($_GET['dept_id']) ? $_GET['dept_id']:'';
		if($d_id){
			$sql = "SELECT class_id,class_name FROM ".DB::table('class')." WHERE isdelete = 0 AND dept_id='".$d_id."' LIMIT 100";
			$result = DB::fetch_all($sql);
			if($result){
				foreach($result AS $key => $value){
					$option .= '<option value="'.$value['d_id'].'" title="'.$value['d_name'].'">'.$value['d_name']."</option>";
				}
			}
		}
		return $option;
	}
    
	/*
	*	获取学校的班级数组
	*/
	public function get_dept_class_array($d_id,$class_year) {
		$dept_class_array='';
		if($d_id && $class_year){
			$sql = "SELECT class_id,class_name,dept_id FROM ".DB::table('class')." WHERE dept_id='".$d_id."' AND class_year='".$class_year."' AND isdelete = 0 LIMIT 200";
			$result = DB::fetch_all($sql);
			if($result){
                $dept_class_array=$result;
				//foreach($result AS $key => $value){
				//	$dept_class_array[$value['class_id']] = $value['class_name'];
				//}
			}
		}
		return $dept_class_array;
	}

	/*
	*	获取当前登录用户是班主任的班级
	*/
	public function get_master_class($user_id='') {
		global $_G;
		$class_array = array();
		//DEBUG 取出班主任所管理的班级
		if(empty($user_id)){
			$user_id = $_G['user_id'];
		}
		$get_my_master_class_sql = "SELECT class_id FROM ".DB::table('class_member')." WHERE isdelete=0 AND cm_type=20 AND user_id='".$user_id."'";
		$get_my_master_class = DB::fetch_all($get_my_master_class_sql);
		foreach($get_my_master_class AS $key => $value){
			$class_array[$value['class_id']] = $value['class_id'];
		}
		$get_my_master_class_sql = "SELECT * FROM ".DB::table('class')." WHERE isdelete = 0 AND class_id IN (".dimplode($class_array).")";
		$get_my_master_class = DB::fetch_all($get_my_master_class_sql);
		return $get_my_master_class;
	}

	/*
	*	MASTER 设置班主任 与 任课老师
	*/
	public function set_cm_type($user_id='') {
		$user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id']:'';
		$cm_type = isset($_REQUEST['cm_type']) ? trim($_REQUEST['cm_type']):'';
		$class_id = isset($_REQUEST['class_id']) ? trim($_REQUEST['class_id']):'';
		if($cm_type && $user_id && $class_id){
			$data = array(
				'cm_type' => $cm_type
			);
			$where = array(
				'user_id' => $user_id,
				'class_id' => $class_id
			);
			DB::update('class_member', $data, $where);
			self::return_json(false,'操作成功');
		}else{
			self::return_json();
		}
	}

	/*
	*	获取当前登录用户所在管理的班级
	*/
	public function get_created_class($user_id='') {
		global $_G; 
		//DEBUG 是否是 班主任 如果是班主任 则取出所管理的班级
		if(empty($user_id)){
			$user_id = $_G['user_id'];
		}
		$get_my_created_class_sql = "SELECT * FROM ".DB::table('class')." WHERE isdelete=0 AND creator_id='".$user_id."'";
		$get_my_created_class = DB::fetch_all($get_my_created_class_sql);
		return $get_my_created_class;
	}

	/*
	*	获取当前登录用户创建 与 担任班主任班级 的合集
	*/
	public function get_created_master_class($user_id='') {
		global $_G;
		if(empty($user_id)){
			$user_id = $_G['user_id'];
		}
		//DEBUG 如果是校管理员 取出机构下所有班级
		if($_G['member']['is_dept_master']){
			$data_merge= self::get_all_class();
		}else{
			$data_1 = self::get_master_class($user_id);
			$data_2 = self::get_created_class($user_id);
			$data_merge = array_merge($data_1,$data_2);
		}
		return $data_merge;
	}

	/*
	*	获取班级成员
	*/
	public function get_class_member($class_id,$cm_type='',$ischeck='') {
		global $_G;
		$wheresql = '';
		$class_member = array();
		if(!empty($class_id)){
			if(!is_array($cm_type) && !empty($cm_type)){
				$wheresql = " AND cm.cm_type = '".$cm_type."' ";
			}elseif(!empty($cm_type)){
				$wheresql = " AND cm.cm_type IN (".dimplode($cm_type).") ";
			}
			if($ischeck){
				$leftjoin = " LEFT JOIN ".DB::table('users')." AS u ON cm.user_id=u.user_id ";
				$wheresql .= " AND u.ischeck=1 ";
			}
			$sql = "SELECT * FROM ".DB::table('class_member')." AS cm ".$leftjoin." WHERE cm.isdelete=0 AND cm.class_id='".$class_id."' ".$wheresql;
			$result = DB::fetch_all($sql);
			//DEBUG 获取班级成员信息
			foreach($result AS $key => $value){
				$class_member[$value['user_id']] = $value;
			}
			$class_member_user_id_array = array_keys($class_member);
			$class_member_hschool_user_info_array = array();
			$hschool_user_class_path = SITE_ROOT.'./source/class/admin/hschool/hschool_user.php';
			if(file_exists($hschool_user_class_path) && !empty($class_member_user_id_array)){
				require_once($hschool_user_class_path);
				$hschool_user = new hschool_user();
				$class_member_hschool_user_info_array = $hschool_user->info($class_member_user_id_array);
				foreach($class_member_hschool_user_info_array AS $key => $value){
					$class_member[$value['user_id']]['info'] = $value;
				}
			}
		}
		return $class_member;
	}

	/*
	*	创建班级
	*/
	public function add() {
		global $_G;
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){
			$class_array = array();
			$data = array(
				'class_name' => isset($_REQUEST['class_name']) ? $_REQUEST['class_name']:'',
                'grade_num' => isset($_REQUEST['grade_num']) ? $_REQUEST['grade_num']:'',
                'class_num' => isset($_REQUEST['class_num']) ? $_REQUEST['class_num']:'',
                'class_year' => isset($_REQUEST['class_year']) ? $_REQUEST['class_year']:'',
                'class_graduate' => isset($_REQUEST['class_graduate']) ? $_REQUEST['class_graduate']:'',
                'creator_id' => $_G['user_id'],
                'dept_id' => isset($_REQUEST['dept_id']) ? $_REQUEST['dept_id']:'',
                'class_invitation_code' => random(6),
				'create_time' => TIMESTAMP
			);
            if(empty($data['dept_id'])){
               self::return_json(true,'操作异常,班级无从属机构学校');die; 
            }
            $insert_id = @DB::insert('class',$data,true);
            /*
			$add_num = 0;
			$class_num = count($class_array);
			foreach($class_array AS $key => $value){
				$data = array(
					'creator_id' => $_G['user_id'],
					'class_name' => trim($value),
					'class_invitation_code' => random(6),
					'create_time' => TIMESTAMP,
					'dept_id' => $_G['dept_id'],
				);
				$insert_id = DB::insert('class',$data,true);
				if($insert_id){
					$add_num++;
				}
			}
            */
			if($insert_id){
				self::return_json(false,'操作成功');
			}else{
				self::return_json();
			}
		}else{
            $dept_id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
			include template('admin/hschool/class_add');	
		}
	}

	/*
	*	创建班级
	*/
	public function edit() {
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){
			$class_id = isset($_REQUEST['class_id']) ? $_REQUEST['class_id']:'';
			$class_name = isset($_REQUEST['class_name']) ? $_REQUEST['class_name']:'';
            $grade_num = isset($_REQUEST['grade_num']) ? $_REQUEST['grade_num']:'';
            $class_num = isset($_REQUEST['class_num']) ? $_REQUEST['class_num']:'';
            $class_year = isset($_REQUEST['class_year']) ? $_REQUEST['class_year']:'';
            $class_graduate = isset($_REQUEST['class_graduate']) ? $_REQUEST['class_graduate']:'';
			$data = array(
				'class_name' => $class_name,
                'grade_num' => $grade_num,
                'class_num' => $class_num,
                'class_year' => $class_year,
                'class_graduate' => $class_graduate,
				'modify_time' => TIMESTAMP,
			);
			$where = array(
				'class_id' => $class_id 	
			);
			DB::update('class',$data,$where);
			self::return_json(false,'操作成功');
		}else{
			//DEBUG 取出一个班级信息
			$class_id = isset($_REQUEST['class_id']) ? $_REQUEST['class_id']:'';
			$info = $this->get_one_class($class_id);
			include template('admin/hschool/class_edit');	
		}
	}

	/*
	*	删除班级
	*/
	public function del() {
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){
			$class_id = isset($_REQUEST['class_id']) ? $_REQUEST['class_id']:'';
			$data = array(
				'isdelete' => 1 	
			);
			$where = array(
				'class_id' => $class_id
			);
			$effects = DB::update('class',$data,$where);
			if($effects){
				self::return_json(false,'操作成功');
			}else{
				self::return_json();
			}
		}
	}

	/*
	*	删除班级
	*/
	public function member_remove() {
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){
			$class_id = isset($_REQUEST['class_id']) ? $_REQUEST['class_id']:'';
			$user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id']:'';
			$data = array(
				'isdelete' => 1 	
			);
			$where = array(
				'user_id' => $user_id,
				'class_id' => $class_id
			);
			$effects = DB::update('class_member',$data,$where);
            //DEBUG 用户表班级信息字段更新
            $where_users = array(
				'user_id' => $user_id
			);
            $effects2 = @DB::update('users',array('class_id'=>0),$where_users);
			if($effects){
				self::return_json(false,'操作成功');
			}else{
				self::return_json();
			}
		}
	}

	/*
	*	当前动作的首页
	*/
	public function index() {
		global $_G;
		$class_id = isset($_REQUEST['class_id']) ? $_REQUEST['class_id']:'';
		$cm_type_string = isset($_REQUEST['cm_type']) ? $_REQUEST['cm_type']:'20,22';
		$cm_type = explode(',',$cm_type_string);
        /*
		$class_array_all = self::get_created_master_class();
		foreach($class_array_all AS $key => $value){
			if($class_id==$value['class_id'] && !empty($class_id)){
				$cur_class_info=$value;
				$cur_class_info['class_member'] = $this->get_class_member($value['class_id'],$cm_type);
			}else{
				if(0==$key){
					$cur_class_info=$value;
					$cur_class_info['class_member'] = $this->get_class_member($value['class_id'],$cm_type);
				}
			}
			$class_array[$value['class_id']] = $value;
		}
        */
        $d_id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
        $class_year = isset($_REQUEST['class_year']) ? $_REQUEST['class_year']:'';
        if($class_year){
            dsetcookie('class_year',$class_year);
        }
        $cookie_class_year = getcookie('class_year');
        if($cookie_class_year){
            $class_year = $cookie_class_year;
        }else{
           $class_year = isset($_REQUEST['class_year']) ? $_REQUEST['class_year']:date('Y');
        }
        $class_array = $cur_class_info = array();
        $class_array = self::get_dept_class_array($d_id,$class_year);
        if($class_id){
            $cur_class_info = $this->get_one_class($class_id);
            $cur_class_info['class_member'] = $this->get_class_member($class_id,$cm_type);
        }else{
            if($class_array[0]){
                $cur_class_info = $this->get_one_class($class_array[0]['class_id']);
                $cur_class_info['class_member'] = $this->get_class_member($class_array[0]['class_id'],$cm_type);
            }
        }
		foreach($cur_class_info['class_member'] AS $key => $value){
			$hschool_user_class_path = SITE_ROOT.'./source/class/admin/hschool/hschool_user.php';
			if(file_exists($hschool_user_class_path)){
				require_once($hschool_user_class_path);
				$hschool_user = new hschool_user();
				$cur_class_info['class_member'][$key]['cm_type_name'] = $hschool_user->get_cm_type_name($value['cm_type']);
			}
			$subject_array = explode(',', $value['subject_d_ids']);
			$subject_array = ext::d_name($subject_array);
			$cur_class_info['class_member'][$key]['subject_d_name'] = implode(',',$subject_array);
		}
        $cache_subject_path = SITE_ROOT.'./data/sysdata/cache_subject.php';
        $subject = array();
        if(file_exists($cache_subject_path)){
            include $cache_subject_path;
        }
		include template('admin/hschool/class');
	}

	/*
	* ajax 返回信息
	*/
	public function return_json($error='true',$message="操作失败") {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		echo json_ext($return_array);
		die;
	}
}
?>